<?php
class SmtpController extends Zend_Controller_Action
{
	public function init()
	{
		Application_Helper_Authenticate::checkLogin($this);
	}

	public function indexAction()
	{
		$smtp = new Application_Model_DbTable_Smtp();
		$this->view->smtps = $smtp->fetchAll();
	}

	public function addAction()
	{	
		$form = new Application_Form_Smtp();
		$this->view->form = $form;
		if ($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData))
			{
				$server = $form->getValue('server');
				$port = $form->getValue('port');
				$username = $form->getValue('username');
				$password = $form->getValue('password');
				$tls = $form->getValue('tls');

				$dbSmtp = new Application_Model_DbTable_Smtp();
				$dbSmtp->addSmtp($server, $port, $username, $password, $tls);

				$this->_helper->redirector('index');

			}
			else
			{
				$form->populate($formData);
			}
		}
	}

	public function editAction()
	{
		$form = new Application_Form_Smtp();
		$form->submit->setLabel('Save');
		$this->view->form = $form;
		if ($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData))
			{
				$id = $form->getValue('id');
				$server = $form->getValue('server');
				$port = $form->getValue('port');
				$username = $form->getValue('username');
				$password = $form->getValue('password');
				$tls = $form->getValue('tls');
					
				$dbSmtp = new Application_Model_DbTable_Smtp();
				$dbSmtp->updateSmtp($id, $server, $port, $username, $password, $tls);
					
				$this->_helper->redirector('index');
			}
			else
			{
				$form->populate($formData);
			}
		}
		else
		{
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$dbSmtp = new Application_Model_DbTable_Smtp();
				$form->populate($dbSmtp->getSmtp($id));
			}
		}
	}
	
	public function deleteAction()
	{
		if ($this->getRequest()->isPost()) {
			$del = $this->getRequest()->getPost('del');
			if ($del == 'Yes') {
				$id = $this->getRequest()->getPost('id');
	
				$dbSmtp = new Application_Model_DbTable_Smtp();
				$dbSmtp->deleteSmtp($id);
			}
			$this->_helper->redirector('index');
		}
		else
		{
			$id = $this->_getParam('id', 0);
				
			$dbSmtp = new Application_Model_DbTable_Smtp();
			$this->view->smtp = $dbSmtp->getSmtp($id);
		}
	}
}
?>